package com.ncmmall.querychannel;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author linxs
 * @param <T>
 */
public final class QueryPage<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private final long totalCount;
	private List<T> result;

	private int pageNo = 1;
	private int pageSize = 10;

	/**
	 * @param totalCount
	 * @param result
	 * @param pageNo
	 * @param pageSize
	 */
	public QueryPage(long totalCount, List<T> result, int pageNo, int pageSize) {
		this.totalCount = totalCount;
		this.result = result;
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}
	
	public QueryPage(long totalCount, List<T> result) {
		this.totalCount = totalCount;
		this.result = result;
	}

	/**
	 * @return
	 */
	public long getTotalCount() {
		return totalCount;
	}

	/**
	 * @return the result
	 */
	public List<T> getResult() {
		return null == result ? Collections.<T> emptyList() : result;
	}
	
	public void setResult(List<T> result) {
		this.result = result;
	}
	
	public int getPageNo() {
		return pageNo;
	}

	public QueryPage<T> setPageNo(int pageNo) {
		this.pageNo = pageNo;
		if (this.pageNo < 1) {
			this.pageNo = 1;
		} 
		return this;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	
	public long getTotalPages() {
		if (totalCount < 0) {
			return -1;
		}

		long count = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			count++;
		}
		return count;
	}
	
	public int getFirst() {
		return ((pageNo - 1) * pageSize) + 1;
	}

	public boolean getIsHasNext() {
		return (pageNo + 1 <= getTotalPages());
	}

	public int getNextPage() {
		if (getIsHasNext()) {
			return pageNo + 1;
		}
		return pageNo;
	}

	public boolean getIsHasPre() {
		return pageNo - 1 >= 1;
	}

	public int getPrePage() {
		if (getIsHasPre()) {
			return pageNo - 1;
		}
		return pageNo;
	}

}